#include "dialog.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QCheckBox>
#include <QRadioButton>
#include <QPlainTextEdit>
#include <QPushButton>
void Dialog::do_checkBoxUnder(bool checked)
{
    QFont font = txtEdit->font();
    font.setUnderline(checked);
    txtEdit->setFont(font);
}

void Dialog::do_checkBoxItalic(bool checked)
{
    QFont font = txtEdit->font();
    font.setItalic(checked);
    txtEdit->setFont(font);
}

void Dialog::do_checkBoxBold(bool checked)
{
    QFont font = txtEdit->font();
    font.setBold(checked);
    txtEdit->setFont(font);
}

void Dialog::do_setColor()
{
    //设置颜色
    QPalette plet = txtEdit->palette();
    if(radioBlack->isChecked())
    {
        plet.setColor(QPalette::Text,Qt::black);
    }
    if(radioRed->isChecked())
    {
        plet.setColor(QPalette::Text,Qt::red);
    }
    if(radioPink->isChecked())
    {
        QColor pink = QColor::fromRgb(255, 192, 203);
        plet.setColor(QPalette::Text,pink);
    }
    txtEdit->setPalette(plet);
}

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    checkBoxUnder = new QCheckBox("下划线");
    checkBoxItalic = new QCheckBox("斜体");
    checkBoxBold = new QCheckBox("加粗");

    //水平多选布局
    QHBoxLayout *Hlay1 = new QHBoxLayout();
    Hlay1->addWidget(checkBoxUnder);
    Hlay1->addWidget(checkBoxItalic);
    Hlay1->addWidget(checkBoxBold);

    //水平单选布局
    radioBlack = new QRadioButton("黑色");
    radioRed = new QRadioButton("红色");
    radioPink = new QRadioButton("粉色");
    //QColor pinkColor = QColor::fromRgb(255, 192, 203);
    QHBoxLayout *Hlay2 = new QHBoxLayout();
    Hlay2->addWidget(radioBlack);
    Hlay2->addWidget(radioRed);
    Hlay2->addWidget(radioPink);

    //文本框
    txtEdit = new QPlainTextEdit;//默认构造函数
    txtEdit->setPlainText("hello world");
    QFont font = txtEdit->font();
    font.setPointSize(20);
    txtEdit->setFont(font);

    btnOk = new QPushButton("确定");
    btnClear = new QPushButton("清空");
    btnClose = new QPushButton("退出");
    QHBoxLayout *Hlay3 = new QHBoxLayout();
    Hlay3->addStretch();
    Hlay3->addWidget(btnOk);
    Hlay3->addWidget(btnClear);
    Hlay3->addWidget(btnClose);

    //布局
    QVBoxLayout *Vlay = new QVBoxLayout();
    Vlay->addLayout(Hlay1);
    Vlay->addLayout(Hlay2);
    Vlay->addWidget(txtEdit);
    Vlay->addLayout(Hlay3);

    setLayout(Vlay);

    //建立信号槽连接
    connect(checkBoxUnder,SIGNAL(clicked(bool)),this,SLOT(do_checkBoxUnder(bool)));
    connect(checkBoxItalic,SIGNAL(clicked(bool)),this,SLOT(do_checkBoxItalic(bool)));
    connect(checkBoxBold,SIGNAL(clicked(bool)),this,SLOT(do_checkBoxBold(bool)));

    connect(radioBlack,SIGNAL(clicked(bool)),this,SLOT(do_setColor()));
    connect(radioRed,SIGNAL(clicked(bool)),this,SLOT(do_setColor()));
    connect(radioPink,SIGNAL(clicked(bool)),this,SLOT(do_setColor()));

    connect(btnOk,SIGNAL(clicked(bool)),this,SLOT(accept()));
    connect(btnClear,SIGNAL(clicked(bool)),this,SLOT(reject()));
    connect(btnClose,SIGNAL(clicked(bool)),this,SLOT(close()));


}

Dialog::~Dialog() {}
